﻿/*==========================
 * Author: TLTHau
 * Create Date: 2013/06/07
 ==========================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SketchTour.Models;
using Ivs.Core.Mapping;
using Ivs.Core.Common.Utilities;

namespace SketchTour.DAL
{
    public class MSGF00DAL
    {
        private EFBaseDao db;
        public MSGF00DAL()
        {
            db = new EFBaseDao();
        }

        #region Search
        public IQueryable<m_city> SearchCity()
        {
            return db.m_city;
        }

        public MSGF00Models Search(int id)
        {
            m_golf s = db.m_golf.FirstOrDefault(model => model.ID == id);
            MSGF00Models result = new MSGF00Models
            {
                ID = s.ID,
                CityCode = s.CityCode,
                GolfName = s.GolfName,
                Tel = s.Tel,
                Fax = s.Fax,
                Address = s.Address,
                CreateDate = s.CreateDate,
                CreateBy = s.CreateBy,
                UpdateDate = s.UpdateDate,
                UpdateBy = s.UpdateBy,
                CityName = s.m_city.Name                
            };
            return result;
        }
        public int Search(MSGF00Models Model, out IEnumerable<MSGF00Models> data)
        {
            int returnCode = 0;
            try
            {
                data = from s in db.m_golf
                       select new MSGF00Models
                       {
                           ID = s.ID,
                           CityCode = s.CityCode,
                           GolfName = s.GolfName,
                           Tel = s.Tel,
                           Fax = s.Fax,
                           Address = s.Address,
                           CreateDate = s.CreateDate,
                           CreateBy = s.CreateBy,
                           UpdateDate = s.UpdateDate,
                           UpdateBy = s.UpdateBy,
                           CityName = s.m_city.Name,
                       };
                if (!Utilities.isNullOrEmpty(Model.GolfName))
                {
                    data = data.Where(a => a.GolfName.ToLower().Contains(Model.GolfName.ToLower()));
                }
                if (!Utilities.isNullOrEmpty(Model.CityCode))
                {
                    data = data.Where(a => a.CityCode.Equals(Model.CityCode));
                }
                if (!Utilities.isNullOrEmpty(Model.Tel))
                {
                    data = data.Where(a => a.Tel.ToLower().Contains(Model.Tel.ToLower()));
                }
                if (!Utilities.isNullOrEmpty(Model.Fax))
                {
                    data = data.Where(a => a.Fax.ToLower().Contains(Model.Fax.ToLower()));
                }
                data = data.OrderByDescending(s => s.GolfName);
            }
            catch (Exception ex)
            {
                returnCode = db.ProcessDbException(ex);
                data = null;
            }
            return returnCode;

        }
        #endregion Search

        #region Insert
        public int Insert(MSGF00Models dto)
        {
            int returnCode = 0;
            try
            {
                m_golf sv = new m_golf();
                sv.ID = dto.ID;
                sv.CityCode = dto.CityCode;
                sv.GolfName = dto.GolfName;
                sv.Tel = dto.Tel;
                sv.Fax = dto.Fax;
                sv.Address = dto.Address;
                sv.CreateDate = dto.CreateDate;
                sv.CreateBy = dto.CreateBy;
                sv.UpdateDate = dto.UpdateDate;
                sv.UpdateBy = dto.UpdateBy;
                db.m_golf.AddObject(sv);
                returnCode = db.Saves();
            }
            catch (Exception ex)
            {
                returnCode = db.ProcessDbException(ex);
            }
            return returnCode;
        }
        #endregion Insert

        #region Update
        public int Update(MSGF00Models dto)
        {
            int returnCode = 0;
            try
            {
                m_golf sv = db.m_golf.FirstOrDefault(v => v.ID == dto.ID);
                sv.CityCode = dto.CityCode;
                sv.GolfName = dto.GolfName;
                sv.Tel = dto.Tel;
                sv.Fax = dto.Fax;
                sv.Address = dto.Address;
                sv.CreateDate = dto.CreateDate;
                sv.CreateBy = dto.CreateBy;
                sv.UpdateDate = dto.UpdateDate;
                sv.UpdateBy = dto.UpdateBy;
                returnCode = db.Saves();
            }
            catch (Exception ex)
            {
                returnCode = db.ProcessDbException(ex);
            }
            return returnCode;
        }
        #endregion Update

        #region Delete
        public int Delete(List<int> listID)
        {
            int returnCode = 0;
            foreach (int id in listID)
            {
                try
                {
                    m_golf sv = db.m_golf.FirstOrDefault(v => v.ID == id);
                    db.m_golf.DeleteObject(sv);
                    returnCode = db.Saves();
                }
                catch (Exception ex)
                {
                    returnCode = db.ProcessDbException(ex);
                }
            }
            return returnCode;
        }
        #endregion Update

    }
}